package edu.uclmgmu.bacteriosec.rmiserver;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class Listener extends UnicastRemoteObject implements IListener {

	private Hashtable<String, Integer> numberOfCalls;
	private Hashtable<String, Integer> numberOfCallInTurns;
	private Hashtable<String, Integer> numberOfCurrentCalls;
	private String currentJoinPoint;

	public Listener() throws RemoteException {
		super();
		LocateRegistry.createRegistry(2995);
	}
	
	public void start() throws RemoteException, MalformedURLException {
		this.numberOfCallInTurns=new Hashtable<String, Integer>();
		this.numberOfCalls=new Hashtable<String, Integer>();
		this.numberOfCurrentCalls=new Hashtable<String, Integer>();
		try {
			Naming.bind("rmi://127.0.0.1:2995/bacteriosecListener", this);
			System.out.println("Listening");
		}
		catch (AlreadyBoundException ex) {
			Naming.rebind("rmi://127.0.0.1:2995/bacteriosecListener", this);
			System.out.println("Listening");
		}
	}
	
	public void stop() throws RemoteException, MalformedURLException, NotBoundException {
		Naming.unbind("rmi://127.0.0.1:2995/bacteriosecListener");
	}

	public void updateNumberOfCalls(String joinPointName) throws RemoteException {
		if (this.numberOfCalls.get(joinPointName)==null) {
			this.numberOfCalls.put(joinPointName, 1);
			return;
		}
		int noc=this.numberOfCalls.get(joinPointName);
		noc=noc+1;
		this.numberOfCalls.put(joinPointName, noc);
	}

	public int getNumberOfCallInTurn(String joinPointName) throws RemoteException  {
		if (this.numberOfCallInTurns.get(joinPointName)==null) {
			this.numberOfCallInTurns.put(joinPointName, 1);
			return 1;
		}
		int result=this.numberOfCallInTurns.get(joinPointName);
		return result;
	}

	public int getNumberOfCurrentCall(String joinPointName) throws RemoteException  {
		int result=this.numberOfCurrentCalls.get(joinPointName);
		return result;
	}

	public void updateNumberOfCurrentCall(String joinPointName) throws RemoteException  {
		int noc=this.numberOfCurrentCalls.get(joinPointName);
		noc++;
		this.numberOfCurrentCalls.put(joinPointName, noc++);
	}

	public String getCurrentjoinPoint() throws RemoteException  {
		return this.currentJoinPoint;
	}

	public int getNumberOfCallsToJP(String jpFile) throws RemoteException  {
		int result=this.numberOfCalls.get(jpFile);
		return result;
	}

	public void saveCurrentJoinPoint(String jpName) throws RemoteException  {
		this.currentJoinPoint=jpName;
	}

	public void createFileForCallInTurn(String joinPointName, int callInTurn) throws RemoteException  {
		this.numberOfCallInTurns.put(joinPointName, callInTurn);
	}

	public int readNumberOfCalls(String jpName) throws RemoteException  {
		return this.numberOfCalls.get(jpName);
	}

	public void createFileForCurrentCall(String joinPointName, int currentCall) throws RemoteException  {
		this.numberOfCurrentCalls.put(joinPointName, currentCall);
	}
	
	public Vector<String> loadFilesNumberOfCalls() throws RemoteException {
		Vector<String> result=new Vector<String>();
		Enumeration<String> e=this.numberOfCalls.keys();
		while (e.hasMoreElements()) {
			result.add(e.nextElement());
		}
		return result;
	}
}
